package app

import (
	"github.com/gin-gonic/gin"
	"net/http"

	"evtest/util/e"
)

type Gin struct {
	C *gin.Context
}

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

// Response setting gin.JSON
func (g *Gin) Response(errCode int, msg string, data interface{}) {
	meta := map[string]interface{}{
		"status": errCode,
		"msg":    msg,
	}
	g.C.JSON(http.StatusOK, gin.H{
		"meta": meta,
		"data": data,
	})
	return
}

func (g *Gin) Error(errCode int) {
	g.ErrorWithMsg(errCode, e.GetMsg(errCode))
	return
}

func (g *Gin) ErrorWithMsg(errCode int, msg string) {
	g.Response(errCode, msg, nil)
	return
}

func (g *Gin) Success(data interface{}) {
	g.Response(e.SUCCESS, e.GetMsg(e.SUCCESS), data)
	return
}
